package com.sgb.kjwl.viewmodel.pick;

import com.sgb.kjwl.api.APIManager;
import com.swgk.core.base.api.ICallBack;
import com.swgk.core.base.model.entity.H5ReEntity;
import com.swgk.core.util.MLog;
import com.swgk.core.util.MToast;
import com.swgk.core.util.PickSelectEntity;
import com.sgb.kjwl.model.entity.ProListEntity;
import com.sgb.kjwl.model.entity.TenderListEntity;
import com.sgb.kjwl.repository.PickProjectRepertory;
import com.sgb.kjwl.view.ui.fragment.pick.PickAttentionSigningUpListFragment;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.SharedPreferenceUtil;
import com.swgk.core.view.areapopup.AddressEntity;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


/**
 * 智能关注-招采-招标
 */
public class PickAttentionSigningUpListModel extends BaseViewModel {

    private final PickAttentionSigningUpListFragment fragment;
    private final PickProjectRepertory repertory;
    private int page = 1;
    private Map map;
    private PickSelectEntity mAll;

    public PickAttentionSigningUpListModel(PickAttentionSigningUpListFragment pickAttentionSigningUpListFragment, PickProjectRepertory pickProjectRepertory) {
        this.fragment = pickAttentionSigningUpListFragment;
        repertory = pickProjectRepertory;
        mAll = new PickSelectEntity();
        mAll.setNo("");
        mAll.setProjectscale("全部");
    }

    public void getNewList(String cityNo, String typeNo, String priceNo, boolean b, String statusNo, String verticalType, String areaCode, String goodsType) {
        map = new HashMap();
        page = b ? page + 1 : 1;
        map.put("limit", 20);
        map.put("page", page);
        map.put("areacode", areaCode);
        map.put("city", cityNo);
        map.put("verticalType", verticalType); // 报名方式：1、资格预审；2、资格后审；3、报价即报名
        map.put("price", priceNo);
        if("0".equals(goodsType)){
            goodsType = "";
        }
        if("0".equals(typeNo)){
            typeNo = "";
        }
        map.put("type", typeNo); // 招标类型
        map.put("goodsType", goodsType); // 清单分类类型
        map.put("state", statusNo); // 状态

        if (fragment.getActivity() != null && !fragment.getActivity().isDestroyed() && !fragment.getActivity().isFinishing()) {
            DialogHelper.showProgressDialog(fragment.getActivity(), null, "加载中", 0, true, null).setCanceledOnTouchOutside(false);
        }
        APIManager.getInstance().getPickAPI().getBidNoticePage(map).enqueue(new Callback<BaseEntity<ProListEntity<List<TenderListEntity>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<ProListEntity<List<TenderListEntity>>>> call, Response<BaseEntity<ProListEntity<List<TenderListEntity>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null)
                    return;
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    fragment.showList(response.body().getData().getList(), b);
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<ProListEntity<List<TenderListEntity>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void getFilterStatusData() {
        map = new HashMap();
        map.put("areacode", repertory.getAreaCode());
        APIManager.getInstance().getPickAPI().getProjectStatus(map).enqueue(new ICallBack<BaseEntity<List<PickSelectEntity>>>() {

            @Override
            public void onSuccess(BaseEntity<List<PickSelectEntity>> data) {
                if (data == null || data.getState() == null) {
                    return;
                }

                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    fragment.setStatusDatas(data.getData());
                }
            }
        });
    }

    public void getTenderNewTypeList() {
        APIManager.getInstance().getPickAPI().getTenderNewTypeList().enqueue(new Callback<BaseEntity<List<PickSelectEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<PickSelectEntity>>> call, Response<BaseEntity<List<PickSelectEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    fragment.setTenderTypeDatas(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<PickSelectEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }
    public void getPriceData() {
        if (fragment.getActivity() != null && !fragment.getActivity().isDestroyed() && !fragment.getActivity().isFinishing()) {
            DialogHelper.showProgressDialog(fragment.getActivity(), null, "加载中", 0, true, null).setCanceledOnTouchOutside(false);
        }
        APIManager.getInstance().getPickAPI().getProjectMoney().enqueue(new Callback<BaseEntity<List<PickSelectEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<PickSelectEntity>>> call, Response<BaseEntity<List<PickSelectEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    fragment.setPriceDatas(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<PickSelectEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void getProvinceData() {
        DialogHelper.showProgressDialog2(fragment.getContext(), null, "加载中", 0, true, null,false);
        APIManager.getInstance().getPickAPI().getAddressEntityCityList().enqueue(new ICallBack<BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>>>() {
            @Override
            public void onSuccess(BaseEntity<List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>>> data) {
                if (data != null && data.getData() != null && data.getData().size() > 0) {
                    fragment.setProvinceDatas(data.getData());
                }
            }
        });
    }

    public void getAddressCityData(List<AddressEntity<List<AddressEntity<List<AddressEntity>>>>> scrData, String no) {
        for (AddressEntity<List<AddressEntity<List<AddressEntity>>>> scrDatum : scrData) {
            if (scrDatum.getId().equals(no)) {
                List<AddressEntity<List<AddressEntity>>> children = scrDatum.getChildren();
                if (children != null && children.size() > 0) {
                    List<PickSelectEntity> entities = new ArrayList<>();
                    for (AddressEntity<List<AddressEntity>> child : children) {
                        entities.add(new PickSelectEntity(child.getNo(), child.getSimpleName()));
                    }
                    entities.add(0, mAll);
                    fragment.setCityDatas(entities);
                }
            }
        }
    }
    public void getFilterTypeData() {
        if (fragment.getActivity() != null && !fragment.getActivity().isDestroyed() && !fragment.getActivity().isFinishing()) {
            DialogHelper.showProgressDialog(fragment.getActivity(), null, "加载中", 0, true, null).setCanceledOnTouchOutside(false);
        }
        APIManager.getInstance().getPickAPI().tenderNewTypeList().enqueue(new Callback<BaseEntity<List<PickSelectEntity>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<PickSelectEntity>>> call, Response<BaseEntity<List<PickSelectEntity>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    fragment.setTypeDatas(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<PickSelectEntity>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public void getTenderGoodsCategoryAllList(String category) {
        if (fragment.getActivity() != null && !fragment.getActivity().isDestroyed() && !fragment.getActivity().isFinishing()) {
            DialogHelper.showProgressDialog(fragment.getActivity(), null, "加载中", 0, true, null).setCanceledOnTouchOutside(false);
        }
        map = new HashMap();
        map.put("type", category);
        APIManager.getInstance().getPickAPI().getTenderGoodsCategoryAllList(map).enqueue(new Callback<BaseEntity<List<PickSelectEntity<List<PickSelectEntity<List<PickSelectEntity>>>>>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<PickSelectEntity<List<PickSelectEntity<List<PickSelectEntity>>>>>>> call, Response<BaseEntity<List<PickSelectEntity<List<PickSelectEntity<List<PickSelectEntity>>>>>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }

                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    fragment.setTenderGoodsCategoryAllList(response.body().getData());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<PickSelectEntity<List<PickSelectEntity<List<PickSelectEntity>>>>>>> call, Throwable t) {
                DialogHelper.dismissProgressDialog();
            }
        });
    }

    public String getAreaCode() {
        return repertory.getAreaCode();
    }

    public void getUrlSettingTender(String id) {
        DialogHelper.showProgressDialog2(fragment.getActivity(), null, "加载中", 0, true, null, false);
        APIManager.getInstance().getPickAPI().getUrlSetting("FrontDeskBidding").enqueue(new ICallBack<BaseEntity<H5ReEntity>>() {
            @Override
            public void onSuccess(BaseEntity<H5ReEntity> data) {
                if (data == null || data.getState() == null) {
                    return;
                }

                if (data.getState().equals(APIManager.RESPONSE_OK)) {
                    String url = data.getData().getUrl() + "?id=" + id;
                    String lng = SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.LONGITUDE_ID, "");
                    String lat = SharedPreferenceUtil.getInstance().getString(SharedPreferenceUtil.LATITUDE_ID, "");
                    url = url + "&lng=" + lng + "&lat=" + lat;
                    MLog.d("api", "H5跳转链接  " + url);
                    fragment.getUrl(url, id);
                } else {
                    MToast.showToast(fragment.getActivity(), data.getMsg());
                }
            }
        });
    }

}
